﻿@using bpms.Enums
@model IEnumerable<OrderView>
@{
    var userTypeClaim = User.FindFirst("UserType")?.Value;
    var userType = !string.IsNullOrEmpty(userTypeClaim)
        ? (UserType)int.Parse(userTypeClaim)
        : UserType.Admin;
}
@{
    ViewData["Title"] = "订单管理";
}

<h2>@ViewData["Title"]</h2>

<!-- 搜索表单 -->
<form asp-action="Index" method="get" class="mb-4">
    <div class="input-group" style="max-width:400px">
        <input type="text"
               name="searchKey"
               class="form-control"
               placeholder="订单号/商品名称"
               value="@ViewBag.SearchKey">
        <button type="submit" class="btn btn-primary">
            <i class="bi bi-search"></i> 搜索
        </button>
        <button type="button"
                id="printBtn"
                class="btn btn-outline-secondary">
            <i class="bi bi-printer"></i> 打印
        </button>
    </div>
</form>

<table class="table table-hover">
    <thead>
        <tr>
            <th>@Html.DisplayNameFor(m => m.OrderNo)</th>
            <th>商品名称</th>
            @if (userType == UserType.Admin)
            {
                <th>@Html.DisplayNameFor(m => m.CustomerName)</th>
            }
            <th>@Html.DisplayNameFor(m => m.Quantity)</th>
            <th>@Html.DisplayNameFor(m => m.TotalAmount)</th>
            <th>@Html.DisplayNameFor(m => m.DeliveryTime)</th>
            <th>@Html.DisplayNameFor(m => m.CreatedTime)</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.OrderNo</td>
                <td>@item.ProductName</td>
                @if (userType == UserType.Admin)
                {
                    <td>@item.CustomerName</td>
                }
                <td>@item.Quantity</td>
                <td>@item.TotalAmount.ToString("C")</td>
                <td>@item.DeliveryTime.ToString("yyyy-MM-dd HH:mm")</td>
                <td>@item.CreatedTime.ToString("yyyy-MM-dd HH:mm")</td>
                <td>
                    <a asp-action="Details"
                       asp-route-id="@item.Id"
                       class="btn btn-sm btn-info">
                        详情
                    </a>
                    <form asp-action="Delete" method="post" class="d-inline">
                        <input type="hidden" name="id" value="@item.Id" />
                        <button type="submit" class="btn btn-sm btn-danger"
                                onclick="return confirm('确定删除？')">
                            <i class="bi bi-trash"></i>
                            删除
                        </button>
                    </form>
                </td>
            </tr>
        }
    </tbody>
</table>


@section Scripts {
    <script>
        $(document).ready(function() {
            // 打印按钮点击事件
            $('#printBtn').click(function() {
                // 克隆表格并移除操作列
                const $table = $('table').clone();
                $table.find('th:last-child, td:last-child').remove();

                // 创建打印内容
                const printContent = `
                    <h3 class="text-center">订单列表</h3>
                    <p class="text-right">打印时间：${new Date().toLocaleString()}</p>
                    ${$table.prop('outerHTML')}
                `;

                // 执行打印
                const printWindow = window.open('', '_blank');
                printWindow.document.write(printContent);
                printWindow.document.close();
                printWindow.print();
            });
        });
    </script>
}